package com.lhyu.mall.controller;

import com.lhyu.mall.domain.*;
import com.lhyu.mall.service.IIndexService;
import com.lhyu.mall.utils.UUIDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class IndexController {
    @Autowired
    private IIndexService indexService;
    @RequestMapping("/indexContent")
    @ResponseBody
    Map<String,Object> index(){
        Map<String,Object> returnMap = new HashMap<>();
        Result result = new Result();
        result.setCode("1");
        result.setDes("成功");
        try {
            List<IndexDomain> list=indexService.quaryContent();
            List<IndexImgUrlsDomain> indexImgUrlsList=indexService.indexImgUrls();
            List<IndexMallNavNewhotItemDomain> navNewhotItemList=indexService.mallNavNewhotItem();
            List<IndexClassBigDomain> classBigList=indexService.indexClassBig();
            returnMap.put("navNewhotItemList",navNewhotItemList);
            returnMap.put("indexImgUrlsList",indexImgUrlsList);
            returnMap.put("list",list);
            returnMap.put("classBigList",classBigList);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode("0");
            result.setDes(e.getMessage());
        }
        returnMap.put("result",result);
        return returnMap;
    }
    @RequestMapping("/classfication")
    @ResponseBody
    Map<String,Object> classfication(){
        Map<String,Object> returnMap = new HashMap<>();
        Result result = new Result();
        result.setCode("1");
        result.setDes("成功");
        try {
            List<ClassficationDomain> classfication=indexService.classfication();
            returnMap.put("classfication",classfication);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode("0");
            result.setDes(e.getMessage());
        }
        returnMap.put("result",result);
        return returnMap;
    }

    @RequestMapping("/cart")
    @ResponseBody
    Map<String,Object> cart(){
        Map<String,Object> returnMap = new HashMap<>();
        Result result = new Result();
        result.setCode("1");
        result.setDes("成功");
        try {
            List<CartDomain> cart=indexService.queryCart();
            Double account = 0.00;
            for(int i=0;i<cart.size();i++){
                if(cart.get(i).isChecked()==true){
                    account +=cart.get(i).getPrice() * cart.get(i).getNums();
                }
            }
            returnMap.put("cart",cart);
            returnMap.put("account",account);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode("0");
            result.setDes(e.getMessage());
        }
        returnMap.put("result",result);
        return returnMap;
    }

    @RequestMapping("/queryComponentGoods")
    @ResponseBody
    Map<String,Object> queryComponentGoods(String viewId){
        Map<String,Object> returnMap = new HashMap<>();
        Result result = new Result();
        result.setCode("1");
        result.setDes("成功");
        try {
            List<ComponentGoodsDomain> componentGoodsList=indexService.queryComponentGoods(viewId);
            returnMap.put("componentGoodsList",componentGoodsList);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode("0");
            result.setDes(e.getMessage());
        }
        returnMap.put("result",result);
        return returnMap;
    }

    @RequestMapping("/queryMessage")
    @ResponseBody
    Map<String,Object> queryMessage(String viewId){
        Map<String,Object> returnMap = new HashMap<>();
        Result result = new Result();
        result.setCode("1");
        result.setDes("成功");
        try {
            List<MessageDomain> queryMessageList=indexService.queryMessage(viewId);
            List<MessageDomain> messageList = new ArrayList<>();
            String str = "yyy-MM-dd HH:mm:ss";
            SimpleDateFormat sdf = new SimpleDateFormat(str);
            for(int i=0;i<queryMessageList.size();i++){
                MessageDomain message = new MessageDomain();
                message.setTimes(sdf.format(queryMessageList.get(i).getTime()));
                message.setDescr(queryMessageList.get(i).getDescr());
                message.setId(queryMessageList.get(i).getId());
                message.setImg(queryMessageList.get(i).getImg());
                message.setNew(queryMessageList.get(i).isNew());
                message.setViewId(queryMessageList.get(i).getViewId());
                messageList.add(message);
            }
            returnMap.put("queryMessageList",messageList);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode("0");
            result.setDes(e.getMessage());
        }
        returnMap.put("result",result);
        return returnMap;
    }

    @RequestMapping("/insertGoods")
    @ResponseBody
    Map<String,Object> insertGoods(String goodId,String img,String name,int num,String unit,String unit2,double price,double oldPrice){
        Map<String,Object> returnMap = new HashMap<>();
        Result result = new Result();
        result.setCode("1");
        result.setDes("成功");
        try {
            List<AddGoodsDomain> list = indexService.isGoodInCart(goodId);
            if(list !=null && list.size()!=0){
                AddGoodsDomain upGoods = new AddGoodsDomain();
                upGoods.setGoodId(list.get(0).getGoodId());
                upGoods.setNums(list.get(0).getNums()+1);
                indexService.updateGoodInCart(upGoods);
            }else{
                AddGoodsDomain addGoods = new AddGoodsDomain();
                addGoods.setId(UUIDGenerator.getUUID());
                addGoods.setGoodId(goodId);
                addGoods.setImg(img);
                addGoods.setName(name);
                addGoods.setNum(num);
                addGoods.setUnit(unit);
                addGoods.setUnit2(unit2);
                addGoods.setPrice(price);
                addGoods.setOldPrice(oldPrice);
                indexService.insertGoods(addGoods);
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setCode("0");
            result.setDes(e.getMessage());
        }
        returnMap.put("result",result);
        return returnMap;
    }
}
